<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="25%" :title="`${drawerProps.title}-字典项管理`">
    <el-form ref="ruleFormRef" label-width="100px" label-suffix=":" :rules="rules" :model="operateDataInfo">
      <el-form-item label="字典标签" prop="label">
        <el-input v-model="operateDataInfo.label" placeholder="请输入字典标签" clearable></el-input>
      </el-form-item>
      <el-form-item label="值类型" prop="moduleName">
        <el-select
          v-model="operateDataInfo.valueType"
          filterable
          placeholder="请选择值类型"
          @change="operateDataInfo.value = ''"
          clearable
        >
          <el-option
            v-for="option in drawerProps.enumMap?.get('valueType')"
            :key="option.value"
            :value="option.value"
            :label="option.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="字典值" prop="value">
        <el-input
          v-if="operateDataInfo.valueType === 1"
          v-model="operateDataInfo.value"
          placeholder="请输入字典值"
          clearable
        ></el-input>
        <el-input
          v-if="operateDataInfo.valueType === 2"
          type="number"
          v-model="operateDataInfo.value"
          placeholder="请输入字典值"
          clearable
        ></el-input>
        <el-switch
          v-if="operateDataInfo.valueType === 3"
          v-model="operateDataInfo.value"
          inline-prompt
          active-text="true"
          active-value="true"
          inactive-text="false"
          inactive-value="false"
        />
        <el-input
          v-if="operateDataInfo.valueType === 4"
          v-model="operateDataInfo.value"
          type="textarea"
          :rows="5"
          placeholder="请输入字典值"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="附加信息" prop="extendJson">
        <el-input
          v-model="operateDataInfo.extendJson"
          type="textarea"
          :rows="10"
          placeholder="附加信息为json格式"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number v-model="operateDataInfo.sort" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { DictItemPageResult, DictItemSaveParam, DictItemUpdateParam, getDictItemInfo } from "@/api/modules/sys/dict";

interface DrawerProps {
  title: string;
  isSave: boolean;
  isUpdate: boolean;
  row: Partial<DictItemPageResult>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
  enumMap?: Map<string, { [key: string]: any }[]>;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  title: "",
  isSave: false,
  isUpdate: false,
  row: {}
});

const rules = reactive({
  label: [{ required: true, message: "请填写字典标签" }],
  value: [{ required: true, message: "请填写字典值" }]
});

const operateDataInfo = ref<DictItemSaveParam | DictItemUpdateParam>({ dictId: "" });
const openDrawer = async (params: DrawerProps) => {
  if (params.isUpdate) {
    // 更新信息，先查询
    let { data } = await getDictItemInfo(params.row.id ?? "");
    operateDataInfo.value = { ...data };
  } else if (params.isSave) {
    // 新增前的默认值
    operateDataInfo.value = { dictId: "", valueType: 1, extendJson: "{\n" + '  "tagType": "primary"\n' + "}" };
  }
  operateDataInfo.value.dictId = params.row.dictId ?? "";
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value.api!(operateDataInfo.value);
      ElMessage.success({ message: `${drawerProps.value.title} 成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  openDrawer
});
</script>

<style scoped lang="scss"></style>
